package com.teach.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.teach.R;
import com.teach.dto.LoginUser;
import com.teach.dto.NewsSearch;
import com.teach.entity.News;
import com.teach.entity.NewsClass;
import com.teach.service.NewsClassService;
import com.teach.service.NewsService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/")
public class NewsController {

	@Autowired
	private NewsClassService service;

	@Autowired
	private NewsService newsService;

	@RequestMapping("/news-index.do")
	public String doIndex(Integer newsClassId, Integer rowMax, String mb, Model model) {
		NewsSearch search = new NewsSearch();
		search.setNewsClassId(newsClassId);
		search.setRowMax(rowMax);

		model.addAttribute("newsClass", service.get(search.getNewsClassId()));
		model.addAttribute("list", newsService.pageSearch(search).getList());
		if (StringUtils.isEmpty(mb)) {
			return "news-index";
		} else {
			return "news-index-" + mb;
		}
	}

	@RequestMapping("/news-{newsClassId}-{current}.html")
	public String doPage(@PathVariable Integer newsClassId, @PathVariable Integer current, Model model) {
		try {
			NewsSearch search = new NewsSearch();
			search.setCurrent(current);
			search.setNewsClassId(newsClassId);
			NewsClass newsClass = service.get(search.getNewsClassId());
			model.addAttribute("newsClass", newsClass);
			model.addAttribute("title", newsClass.getNewsClassName());
			model.addAttribute("p", newsService.pageSearch(search));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "news/news-page";
	}

	@RequestMapping("/news-show-{newsId}.html")
	public String doShow(@PathVariable Long newsId, Model model) {
		News news = newsService.get(newsId);
		model.addAttribute("news", news);
		model.addAttribute("title", news.getNewsTitle());
		model.addAttribute("newsClass", service.get(news.getNewsClassId()));
		return "news/news";
	}

	/**
	 * 发表文章
	 * 
	 * @param newsTitle
	 * @param newsWorker
	 * @param newsClassId
	 * @param newsContent
	 * @param session
	 * @return
	 */
	@RequestMapping("/news-add.do")
	@ResponseBody
	public R doAdd(News news, LoginUser user) {
		Long i = newsService.getByNewsTitleCount(news.getNewsTitle());
		if (i == 0) {
			String str = news.getNewsContent();
			String s1 = null;
			List<String> img = new ArrayList<>();
			while (str.indexOf("src") != -1) {
				s1 = str.substring(str.indexOf("src") + 5);
				str = s1;
				s1 = s1.substring(0, s1.indexOf("\""));
				img.add(s1);
			}
			if (img != null && !img.isEmpty()) {
				news.setNewsImgs(img.toArray(new String[img.size()]));
			}
			news.setNewsManager(user.getUserName());
			news.setNewsStatus(0);
			newsService.add(news);
			return R.ok();
		} else {
			return R.error("文章标题重复");
		}
	}

	/**
	 * 审核中的文章
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/news-approval.do")
	public String doStatus0(Model model) {
		NewsSearch search = new NewsSearch();
		search.setNewsStatus(0);
		model.addAttribute("s", search.getNewsStatus());
		model.addAttribute("p", newsService.pageSearch(search));
		return "news/news-status";
	}

	/**
	 * 驳回的文章
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/news-reject.do")
	public String doStatus1(Model model) {
		NewsSearch search = new NewsSearch();
		search.setNewsStatus(1);
		model.addAttribute("s", search.getNewsStatus());
		model.addAttribute("p", newsService.pageSearch(search));
		return "news/news-status";
	}

	/**
	 * 我发表的文章
	 * 
	 * @param model
	 * @param session
	 * @param current
	 * @return
	 */
	@RequestMapping("/news-my.do")
	public String doMyNews(Model model, LoginUser user, NewsSearch search) {
		search.setNewsManager(user.getUserName());
		search.setNewsStatus(2);
		model.addAttribute("p", newsService.pageSearch(search));
		return "news/news-my";
	}

	/**
	 * 展示点击修改的文章
	 * 
	 * @param newsId
	 * @param model
	 * @return
	 */
	@RequestMapping("/news-modify-{newsId}.do")
	public String doModifyStart(@PathVariable Long newsId, Model model) {
		model.addAttribute("news", newsService.get(newsId));
		return "news/news-modify";
	}

	/**
	 * 修改文章
	 * 
	 * @param news
	 * @return
	 */
	@RequestMapping("/news-modify-end-{newsId}.do")
	@ResponseBody
	public R doModifyEnd(News news, @PathVariable Long newsId) {
		String str = news.getNewsContent();
		String s1 = null;
		List<String> img = new ArrayList<>();
		while (str.indexOf("src") != -1) {
			s1 = str.substring(str.indexOf("src") + 5);
			str = s1;
			s1 = s1.substring(0, s1.indexOf("\""));
			img.add(s1);
		}
		if (img != null && !img.isEmpty()) {
			news.setNewsImgs(img.toArray(new String[img.size()]));
		}
		news.setNewsId(newsId);
		news.setNewsStatus(0);
		newsService.modify(news);
		return R.ok();
	}

	/**
	 * 在我发表的文章中搜索
	 * 
	 * @param search
	 * @return
	 */
	@RequestMapping("/news-my-search.html")
	@ResponseBody
	public PageInfo<News> doMySearch(NewsSearch search, LoginUser user) {
		search.setNewsManager(user.getUserName());
		search.setNewsStatus(2);
		return newsService.pageSearchAdmin(search);
	}

	/**
	 * 在审批中的文章搜索
	 * 
	 * @param search
	 * @return
	 */
	@RequestMapping("/news-approval-search.html")
	@ResponseBody
	public PageInfo<News> doStatus(LoginUser user, NewsSearch search) {
		search.setNewsStatus(0);
		search.setNewsManager(user.getUserName());
		return newsService.pageSearchAdmin(search);
	}

	/**
	 * 在驳回的文章搜索
	 * 
	 * @param search
	 * @return
	 */
	@RequestMapping("/news-reject-search.html")
	@ResponseBody
	public PageInfo<News> doRejectStatus(LoginUser user, NewsSearch search) {
		search.setNewsStatus(1);
		search.setNewsManager(user.getUserName());
		return newsService.pageSearchAdmin(search);
	}

	@RequestMapping("/news123.html")
	@ResponseBody
	public PageInfo<News> doStatuss() {
		NewsSearch search = new NewsSearch();
		search.setNewsClassId(6);
		log.debug("search ==>{}", search);
		return newsService.pageSearch(search);
	}

	@RequestMapping("/news-index1.do")
	@ResponseBody
	public Object doindex(Integer current, String newsContent) {
		NewsSearch search = new NewsSearch();
		search.setCurrent(current);
		search.setNewsContent(newsContent);
		return newsService.pageSearch(search);

	}

	@RequestMapping("/news-index2.do")
	@ResponseBody
	public Object doindex(Integer current, Integer rowMax, Integer newsClassId) {
		NewsSearch search = new NewsSearch();
		search.setCurrent(current);
		search.setRowMax(5);
		search.setNewsClassId(newsClassId);
		return newsService.pageSearch(search);

	}

	@RequestMapping("/news-img.do")
	@ResponseBody
	public Object doimg() {
		return newsService.getlist();

	}

	@RequestMapping("news-index-{newsClassId}.html")
	public String doIndexClass() {
		return "news/news-index2";
	}

	@RequestMapping("/news-index-show.do")
	@ResponseBody
	public Object doindexClass(Integer newsClassId) {
		NewsSearch search = new NewsSearch();
		search.setNewsClassId(newsClassId);
		return newsService.pageSearch(search);

	}

	@RequestMapping("approval-news-list.do")
	@ResponseBody
	public Object doApprovalList(NewsSearch search, int status) {
		search.setNewsStatus(status);
		return newsService.pageSearchAdmin(search);
	}

	@RequestMapping("approval-news.do")
	@ResponseBody
	public Object doApproval(Long newsId, int status) {
		try {
			News news = new News();
			news.setNewsId(newsId);
			news.setNewsStatus(status);
			newsService.modify(news);
			return R.ok();
		} catch (Exception e) {
			return R.error("审批失败");
		}
	}

	@RequestMapping("news-manage.do")
	@ResponseBody
	public Object doManageList(NewsSearch search) {
		return newsService.pageSearchAdmin(search);
	}

	@RequestMapping("news-manage-delete.do")
	@ResponseBody
	public Object doManageDelete(Long newsId) {
		try {
			newsService.delete(newsId);
			return R.ok();
		} catch (Exception e) {
			return R.error();
		}

	}
}
